<template>
  <!-- 部门添加弹框 -->
  <el-dialog
    title="添加部门"
    :visible.sync="isShow"
    width="500px"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="95px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" @focus="focusEvent">
          <el-option
            v-for="i in managerList"
            :key="i.id"
            :label="i.username"
            :value="i.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="cancelEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysUserSimple, companydepartmentPost } from '@/api/department.js'
export default {
  props: {
    initList: {
      type: Array,
      default: () => []
    }
  },

  data() {
    return {
      isShow: false,
      mode: 'add',
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      item: '',
      managerList: [],
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 1,
            max: 50,
            message: '请填入指定框的必要名称',
            trigger: 'change'
          },
          {
            // 自定义规则  寻找添加部门中相同的字段 若相同 则抛出警告
            validator: (rule, value, callback) => {
              const resultArr = this.initList.filter((i) => {
                if (i.pid === this.item.id) {
                  return true
                }
              })
              const bol = resultArr.some((i) => {
                return i.name === value
              })
              bol
                ? callback(new Error('请不要输入重复的部门名:' + value))
                : callback()
            }
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 1,
            max: 50,
            message: '请填入指定框的必要名称',
            trigger: 'change'
          },
          {
            // 和所有code项进行比较 不可重复
            validator: (rule, value, callback) => {
              const bol = this.initList.some((i) => {
                return i.code === value
              })
              bol
                ? callback(new Error('请不要输入重复的code编码:' + value))
                : callback()
            }
          }
        ],
        manager: [{ required: true, message: '必填', trigger: 'change' }],
        introduce: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 1,
            max: 50,
            message: '请填入指定框的必要名称',
            trigger: 'change'
          }
        ]
      }
    }
  },
  mounted() {
    this.$bus.$on('addEvent', (show, item) => {
      this.isShow = show
      this.item = item
    })
  },
  beforeDestory() {
    this.$bus.$off('addEvent')
  },
  methods: {
    cancelEvent() {
      this.isShow = false
    },
    async focusEvent() {
      if (this.managerList.length === 0) {
        const res = await sysUserSimple()
        this.managerList = res.data
        console.log('员工列表', res)
      }
    },
    // 重置表单数据第二种方式
    closeEvent() {
      this.$refs.form.resetFields()
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          this.form.pid = this.item.id
          await companydepartmentPost(this.form)
          this.isShow = false
          this.$emit('getData')
          this.$message.success('添加成功')
        }
      })
    }
  }
  // 重置表单数据
  // watch: {
  //   isShow: {
  //     handler (newval) {
  //       if (!newval) {
  //         this.$refs.form.resetFields()
  //       }
  //     },
  //     deep: true,
  //     immediate: true
  //   }
  // }
}
</script>

<style></style>
